package com.example.book.controller;

import com.example.book.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/7
 * Time: 10:03
 */

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpServletRequest request) {
        log.info("接收到参数: " + name);
        HttpSession session = request.getSession(true);
        // 1. 校验参数格式

        // 2. 从数据库中校验账号密码是否正确

        // 3. 如果正确，存储 session，返回 true
        Boolean result = userService.checkUserAndPassword(name, password, session);
        log.info("用户登陆结果, name: {}, 结果:{}", name, result);
        return result;
    }
}
